package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.domain.YddCate;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author pk
 * @date 2022/09/06/19:54
 */
@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class CateController {

    @Autowired
    private YddCateService CateService;


    /**
     * 分类列表-分页
     * @param page
     * @param limit
     * @param catename
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "cateCache",keyGenerator = "commonKeyGenerator")
    public Result list(Integer page,Integer limit,String catename ){
        //创建page对象
        Page pageInfo = new Page(page,limit);
        //条件构造器
        LambdaQueryWrapper<YddCate> lqw = new LambdaQueryWrapper<>();
        lqw.like(catename!=null,YddCate::getCatename,catename);
        lqw.orderByAsc(YddCate::getSortNum);
        CateService.page(pageInfo,lqw);

        List<YddCate> records = pageInfo.getRecords();
        //创建map集合容器
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("totalPages",pageInfo.getPages());
        map.put("pageSize",limit);
        map.put("currentPage",page);
        map.put("data",records);


        return Result.ok(map);

    }


    /**
     * 分类详情
     * 根据id查询分类,修改功能的数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    @Cacheable(value = "cateCache",key = "#id")
    public Result rowInfo(Integer id){
        YddCate yddCate = CateService.getById(id);

        return Result.ok(yddCate);

    }

    /**
     * 分类删除
     * @return
     */
    @GetMapping("/del")
    @CacheEvict(value = "cateCache",allEntries = true)
    public Result del(Integer id){
        CateService.removeById(id);
        return Result.ok("分类删除成功");

    }


    /**
     * 分类列表-全部
     * @return
     */
    @GetMapping("/all")
    @Cacheable(value = "cateCache",keyGenerator = "commonKeyGenerator")
    public Result all(){
        List<YddCate> list = CateService.list();

        return Result.ok(list);

    }

    /**
     * 分类添加
     * @param cate
     * @return
     */
    @PostMapping("/add")
    @CacheEvict(value = "cateCache",allEntries = true)
    public Result add(@RequestBody YddCate cate){

        log.info(cate.toString());

        CateService.save(cate);


        return Result.ok("添加成功");
    }


    /**
     * 分类修改
     * @param cate
     * @return
     */
    @PostMapping("/update")
    @CacheEvict(value = "cateCache",allEntries = true)
    public Result update(@RequestBody YddCate cate){

        CateService.updateById(cate);
        return Result.ok("修改成功");
    }
}
